home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / METAKIT.ZIP / INCLUDE / K4TABLE.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-09  |  7.9 KB  |  247 lines

  1. //@doc      TABLE HDR CORE
  2. //@module   TABLE.H - Table declarations |
  3. //  
  4. //  This file contains the declaration of the table classes.
  5. //  
  6. //@normal   Copyright <cp> 1996 Meta Four Software. All rights reserved.
  7.  
  8. #ifndef __K4TABLE_H__
  9. #define __K4TABLE_H__
  10.  
  11. #ifndef __K4CONF_H__
  12.     #error Please include "k4conf.h" before this header file
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // Declarations in this file
  17.     
  18.     class c4_Bytes;                     // used to pass around generic data
  19.     class c4_Column;                    // a column in a table
  20.     class c4_Table;                     // a table of data
  21.  
  22.     class c4_Field;                     // not defined here
  23.     class c4_Streamer;                  // not defined here
  24.     class c4_Persist;                   // not defined here
  25.     class c4_TableEntry;                // not defined here
  26.     class c4_Sequence;                  // not defined here
  27.     class c4_HandlerSeq;                // not defined here
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. /*@class These fellows know how to clean up their act when they go away.
  31.  
  32.     These objects are used to pass around untyped data without concerns about
  33.     clean-up. They know whether the bytes need to be de-allocated when these
  34.     objects go out of scope. Small amounts of data are stored in the object.
  35. */
  36.  
  37. class c4_Bytes
  38. {
  39.     typedef unsigned char uchar;
  40.  
  41.     uchar* _contents;
  42.     int _size;
  43.     bool _copy;
  44.  
  45.     enum { kMaxBuf = 16 };
  46.     uchar _buffer [kMaxBuf];
  47.  
  48. public:
  49.     //@cmember Constructs an empty object.
  50.     c4_Bytes ();
  51.     //@cmember Constructs an object with contents, no copy.
  52.     c4_Bytes (const void*, int);
  53.     //@cmember Constructs an object with contents, optionally as a copy.
  54.     c4_Bytes (const void*, int, bool);
  55.     //@cmember Copy constructor.
  56.     c4_Bytes (const c4_Bytes&);
  57.     //@cmember Destructor.
  58.     ~c4_Bytes ();
  59.     
  60.     //@cmember Assignment, this may make a private copy of contents.
  61.     c4_Bytes& operator= (const c4_Bytes&);
  62.     //@cmember Swaps the contents and ownership of two byte objects.
  63.     void Swap(c4_Bytes&);
  64.     
  65.     //@cmember Returns the number of bytes of its contents.
  66.     int Size() const;
  67.     //@cmember Returns a pointer to the contents.
  68.     const uchar* Contents() const;
  69.     
  70.     //@cmember Defines contents as a freshly allocated buffer of given size.
  71.     uchar* SetBuffer(int);
  72.     //@cmember Allocates a buffer and fills its contents with zero bytes.
  73.     uchar* SetBufferClear(int);
  74.  
  75.     //@cmember Returns true if the contents of both objects is equal.
  76.     friend bool operator== (const c4_Bytes&, const c4_Bytes&);
  77.     //@cmember Returns true if the contents of both objects is not equal.
  78.     friend bool operator!= (const c4_Bytes&, const c4_Bytes&);
  79.  
  80. private:
  81.     void _MakeCopy();
  82.     void _LoseCopy();
  83. };
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. /*@class Objects of this class define the column contents of tables.
  87.  
  88. @xref <c c4_Table>
  89. */
  90.  
  91. class c4_Column
  92. {
  93.     typedef unsigned char uchar;
  94.     typedef unsigned long ulong;
  95.  
  96.     c4_PtrArray _segments;
  97.     ulong _position;
  98.     ulong _size;
  99.     
  100. public: //@access Public members
  101.     enum { kSegBits = 12, kSegMax = 1 << kSegBits, kSegMask = kSegMax - 1 };
  102.  
  103.     //@cmember Allocate a new buffer of the specified size.
  104.     void SetBuffer(ulong);
  105.     //@cmember Allocate and clear a new buffer of the specified size.
  106.     void SetBufferClear(ulong);
  107.  
  108.     //@cmember Returns the number of bytes as stored on disk.
  109.     ulong Size() const;
  110.     //@cmember Sets a flag to save this column on commit.
  111.     void SetDirty();
  112.     //@cmember Returns true if contents needs to be saved.
  113.     bool IsDirty() const;
  114.  
  115.     //@cmember Special access for the DUMP program.
  116.     ulong Position() const { return _position; }
  117.  
  118.     //@cmember Make sure the data is loaded into memory.
  119.     uchar* LoadNow(c4_Persist*, ulong, int* =0);
  120.     //@cmember Resize the buffer by inserting or removing space.
  121.     void Resize(c4_Persist*, ulong, long);
  122.     //@cmember Save the buffer if dirty, also writes header.
  123.     bool SaveNow(c4_Streamer&);
  124.  
  125.     //@cmember Removes spare memory area and deallocates the cache.
  126.     static void CleanupCache();
  127.  
  128. private:
  129.     c4_Column ();
  130.     ~c4_Column ();
  131.  
  132.     static int Index(ulong);
  133.     uchar* Contents(ulong) const;
  134.  
  135.     static uchar* NewSegment();
  136.     
  137.     friend class c4_Table;
  138. };
  139.  
  140. /////////////////////////////////////////////////////////////////////////////
  141. /*@class Tables organize the data and know their own structure and context.
  142. */
  143.  
  144. class c4_Table
  145. {
  146.     typedef unsigned char uchar;
  147.     typedef unsigned long ulong;
  148.  
  149.     c4_PtrArray _columns;
  150.     c4_Persist* _persist;
  151.     c4_Field* _field;   
  152.     c4_Table* _owner;
  153.     int _ownerRow;
  154.     c4_HandlerSeq* _sequence;
  155.  
  156. public: 
  157. //@group Construction / destruction / initialization
  158.     //@cmember Constructs a new object.
  159.     c4_Table ();
  160.     //@cmember Destructor.
  161.     ~c4_Table ();
  162.  
  163.     //@cmember Initializes this table as a root table.
  164.     void DefineRoot(const char*, c4_Persist* =0);
  165.     //@cmember Initializes this table as a nested table.
  166.     void DefineSub(c4_Field&, c4_Table&, int);
  167.     //@cmember Restructures a table to match the given field layout.
  168.     void Restructure(c4_Field&);
  169.  
  170. //@group Table properties
  171.     //@cmember Returns the associated storage, if any.
  172.     c4_Persist* Persist() const;
  173.     //@cmember Returns the repeating field definition.
  174.     c4_Field& Definition() const;
  175.     //@cmember Returns a reference to the owner of this table.
  176.     c4_Table& Owner() const;
  177.     //@cmember Returns the row index in the owning table.
  178.     int OwnerRow() const;
  179.     //@cmember Returns the column index in the owning table.
  180.     int OwnerColumn() const;
  181.     
  182. //@group Table dimensions
  183.     //@cmember Returns the number of rows in this table.
  184.     int NumRows() const;
  185.     //@cmember Returns the number of columns in this table.
  186.     int NumColumns() const;
  187.  
  188. //@group Access to subfields
  189.     //@cmember Returns the field definition of a subcolumn.
  190.     c4_Field& Field(int) const;
  191.     //@cmember Returns a specific columns of this table.
  192.     c4_Column& Column(int) const;
  193.     //@cmember Returns true if a column consists of subtables.
  194.     bool IsNested(int) const;
  195.     //@cmember Returns the N-th subtable of a subcolumn, creates if necessary.
  196.     c4_Table& SubTable(int, int);
  197.     
  198. //@group Table persistence
  199.     //@cmember Saves current table state to file.
  200.     bool Commit(c4_Streamer&, int =0);
  201.     //@cmember Loads table state from file.
  202.     void Prepare(c4_Streamer&);
  203.     //@cmember Loads one column on demand.
  204.     uchar* LoadColumn(int, ulong, int&) const;
  205.     //@cmember Resizes one column.
  206.     void ResizeColumn(int, ulong, int) const;
  207.  
  208. //@group Internal methods
  209.     //@cmember Sets the row count internally.
  210.     void SetNumRows(int);
  211.     //@cmember Adjust owner references.
  212.     void FixOwnerRows(int, int);
  213.     //@cmember Permanently switch the byte orde.
  214.     void FlipAllBytes();
  215.     //@cmember Returns a reference to the associated sequence.
  216.     c4_HandlerSeq& Sequence();
  217.     //@cmember Replaces the specified subentry with a new sequence.
  218.     void ReplaceEntry(int colNum_, int index_, c4_Sequence* seq_);
  219.  
  220. private:
  221.     c4_Table (const c4_Table&);         // not implemented
  222.     void operator= (const c4_Table&);   // not implemented
  223.  
  224.     c4_TableEntry* SubEntry(int, int) const;
  225.  
  226.     void InitSequence();
  227.     void DetachSequence();
  228.     void SetOwnerRow(int);
  229.     void DeleteColumn(int, int);
  230.     void Reset();
  231.  
  232.     void PrepareColumn(int, c4_Streamer&);
  233.     void PrepareSubTables(int, c4_Streamer&);
  234. };
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237.  
  238. #if q4_INLINE
  239.     #include "k4table.inl"
  240. #endif
  241.  
  242. /////////////////////////////////////////////////////////////////////////////
  243.  
  244. #endif
  245.  
  246. // $Id: k4table.h,v 1.8 1996/12/08 11:22:33 jcw Exp $
  247.